package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.ReportMapper;
import com.tanhua.model.domain.Report;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 灵魂测试的数据层实现类
 *
 * @author 李磊
 * @since 2022-01-14 13:45
 */
@DubboService
public class ReportApiImpl implements ReportApi {


    @Autowired
    ReportMapper reportMapper;

    /**
     * 说明:  插入一条新的报告数据
     *
     * @param report 对象封装
     * @author 李磊
     * @since 2022-01-14 17:57
     */
    @Override
    public void addReport(Report report) {
        reportMapper.insert(report);
    }

    /**
     * 说明:  查询报告
     *
     * @param userId          用户id
     * @param questionnaireId 问卷id
     * @return com.tanhua.model.domain.Report
     * @author 李磊
     * @since 2022-01-15 15:58
     */
    @Override
    public Report findByUserId(Long userId, Long questionnaireId) {
        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Report::getUserId, userId).eq(Report::getQuestionnaireId, questionnaireId).orderByDesc(Report::getUpdated);
        Page<Report> page = new Page<>(1, 1);
        return reportMapper.selectPage(page,wrapper).getRecords().get(0);
    }

    /**
     * 说明:  查询分数范围内的所有报告
     *
     * @param maxScore 上限
     * @param minScore 下限
     * @return java.util.List<com.tanhua.model.domain.Report>
     * @author 李磊  大于等于
     * @since 2022-01-15 20:47
     */
    @Override
    public List<Report> findByScore(int maxScore, int minScore) {
        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        //降序排列
        wrapper.ge(Report::getReportScore, minScore).le(Report::getReportScore, maxScore).orderByDesc(Report::getUpdated);
        return reportMapper.selectList(wrapper);
    }

    /**
     * 说明:  根据id进行报告查询
     *
     * @param reportId 报告id
     * @return com.tanhua.model.domain.Report
     * @author 李磊
     * @since 2022-01-15 22:17
     */
    @Override
    public Report findById(Long reportId) {
        return reportMapper.selectById(reportId);
    }


}
